home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / DistUpgrade / utils.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  8.2 KB  |  258 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from gettext import gettext as _
  5. import locale
  6. import os
  7. import apt_pkg
  8. import urllib2
  9.  
  10. def country_mirror():
  11.     ''' helper to get the country mirror from the current locale '''
  12.     lang_mirror = {
  13.         'c': '' }
  14.     if not os.environ.has_key('LANG'):
  15.         return ''
  16.     lang = os.environ['LANG'].lower()
  17.     if lang_mirror.has_key(lang[:5]):
  18.         return lang_mirror[lang[:5]]
  19.     if '_' in lang:
  20.         country = lang.split('.')[0].split('_')[1]
  21.         return country + '.'
  22.     return lang[:2] + '.'
  23.     return ''
  24.  
  25.  
  26. def get_dist():
  27.     ''' return the codename of the current runing distro '''
  28.     Popen = Popen
  29.     PIPE = PIPE
  30.     import subprocess
  31.     p = Popen([
  32.         'lsb_release',
  33.         '-c',
  34.         '-s'], stdout = PIPE)
  35.     res = p.wait()
  36.     if res != 0:
  37.         sys.stderr.write('lsb_release returned exitcode: %i\n' % res)
  38.         return 'unknown distribution'
  39.     dist = p.stdout.readline().strip()
  40.     return dist
  41.  
  42.  
  43. def url_downloadable(uri, debug_func = None):
  44.     '''
  45.   helper that checks if the given uri exists and is downloadable
  46.   (supports optional debug_func function handler to support 
  47.    e.g. logging)
  48.  
  49.   Supports http (via HEAD) and ftp (via size request)
  50.   '''
  51.     if debug_func:
  52.         debug_func('url_downloadable: %s' % uri)
  53.     
  54.     import urlparse
  55.     (scheme, netloc, path, querry, fragment) = urlparse.urlsplit(uri)
  56.     if scheme == 'http':
  57.         import httplib
  58.         
  59.         try:
  60.             c = httplib.HTTPConnection(netloc)
  61.             c.request('HEAD', path)
  62.             res = c.getresponse()
  63.             if debug_func:
  64.                 debug_func("url_downloadable result '%s'" % res.status)
  65.             
  66.             res.close()
  67.             if res.status == 200:
  68.                 return True
  69.         except Exception:
  70.             e = None
  71.             debug_func("error from httplib: '%s'" % e)
  72.             return False
  73.         
  74.  
  75.     None<EXCEPTION MATCH>Exception
  76.     if scheme == 'ftp':
  77.         import ftplib
  78.         
  79.         try:
  80.             f = ftplib.FTP(netloc)
  81.             f.login()
  82.             f.cwd(os.path.dirname(path))
  83.             size = f.size(os.path.basename(path))
  84.             f.quit()
  85.             if debug_func:
  86.                 debug_func('ftplib.size() returned: %s' % size)
  87.             
  88.             if size != 0:
  89.                 return True
  90.         except Exception:
  91.             e = None
  92.             if debug_func:
  93.                 debug_func("error from ftplib: '%s'" % e)
  94.             
  95.             return False
  96.         
  97.  
  98.     None<EXCEPTION MATCH>Exception
  99.     return False
  100.  
  101.  
  102. def init_proxy(gconfclient = None):
  103.     ''' init proxy settings 
  104.  
  105.   * first check for http_proxy environment (always wins),
  106.   * then check the apt.conf http proxy, 
  107.   * then look into synaptics conffile
  108.   * then into gconf  (if gconfclient was supplied)
  109.   '''
  110.     SYNAPTIC_CONF_FILE = '/root/.synaptic/synaptic.conf'
  111.     proxy = None
  112.     apt_pkg.InitConfig()
  113.     if apt_pkg.Config.Find('Acquire::http::Proxy') != '':
  114.         proxy = apt_pkg.Config.Find('Acquire::http::Proxy')
  115.     elif os.path.exists(SYNAPTIC_CONF_FILE):
  116.         cnf = apt_pkg.newConfiguration()
  117.         apt_pkg.ReadConfigFile(cnf, SYNAPTIC_CONF_FILE)
  118.         use_proxy = cnf.FindB('Synaptic::useProxy', False)
  119.         if use_proxy:
  120.             proxy_host = cnf.Find('Synaptic::httpProxy')
  121.             proxy_port = str(cnf.FindI('Synaptic::httpProxyPort'))
  122.             if proxy_host and proxy_port:
  123.                 proxy = 'http://%s:%s/' % (proxy_host, proxy_port)
  124.             
  125.         
  126.     elif gconfclient:
  127.         
  128.         try:
  129.             if gconfclient.get_bool('/system/http_proxy/use_http_proxy'):
  130.                 host = gconfclient.get_string('/system/http_proxy/host')
  131.                 port = gconfclient.get_int('/system/http_proxy/port')
  132.                 use_auth = gconfclient.get_bool('/system/http_proxy/use_authentication')
  133.                 if host and port:
  134.                     if use_auth:
  135.                         auth_user = gconfclient.get_string('/system/http_proxy/authentication_user')
  136.                         auth_pw = gconfclient.get_string('/system/http_proxy/authentication_password')
  137.                         proxy = 'http://%s:%s@%s:%s/' % (auth_user, auth_pw, host, port)
  138.                     else:
  139.                         proxy = 'http://%s:%s/' % (host, port)
  140.                 
  141.         except Exception:
  142.             e = None
  143.             print 'error from gconf: %s' % e
  144.         except:
  145.             None<EXCEPTION MATCH>Exception
  146.         
  147.  
  148.     None<EXCEPTION MATCH>Exception
  149.     if proxy:
  150.         if not proxy.startswith('http://'):
  151.             return None
  152.         proxy_support = urllib2.ProxyHandler({
  153.             'http': proxy })
  154.         opener = urllib2.build_opener(proxy_support)
  155.         urllib2.install_opener(opener)
  156.         os.putenv('http_proxy', proxy)
  157.     
  158.  
  159.  
  160. def _inhibit_sleep_old_interface():
  161.     '''
  162.   Send a dbus signal to org.gnome.PowerManager to not suspend
  163.   the system, this is to support upgrades from pre-gutsy g-p-m
  164.   '''
  165.     import dbus
  166.     bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
  167.     devobj = bus.get_object('org.gnome.PowerManager', '/org/gnome/PowerManager')
  168.     dev = dbus.Interface(devobj, 'org.gnome.PowerManager')
  169.     cookie = dev.Inhibit('UpdateManager', 'Updating system')
  170.     return (dev, cookie)
  171.  
  172.  
  173. def _inhibit_sleep_new_interface():
  174.     '''
  175.   Send a dbus signal to gnome-power-manager to not suspend
  176.   the system
  177.   '''
  178.     import dbus
  179.     bus = dbus.Bus(dbus.Bus.TYPE_SESSION)
  180.     devobj = bus.get_object('org.freedesktop.PowerManagement', '/org/freedesktop/PowerManagement/Inhibit')
  181.     dev = dbus.Interface(devobj, 'org.freedesktop.PowerManagement.Inhibit')
  182.     cookie = dev.Inhibit('UpdateManager', 'Updating system')
  183.     return (dev, cookie)
  184.  
  185.  
  186. def inhibit_sleep():
  187.     '''
  188.   Send a dbus signal to power-manager to not suspend
  189.   the system, try both the new freedesktop and the
  190.   old gnome dbus interface
  191.   '''
  192.     
  193.     try:
  194.         return _inhibit_sleep_old_interface()
  195.     except Exception:
  196.         e = None
  197.         
  198.         try:
  199.             return _inhibit_sleep_new_interface()
  200.         except Exception:
  201.             e = None
  202.             return (False, False)
  203.         
  204.  
  205.         None<EXCEPTION MATCH>Exception
  206.  
  207.  
  208.  
  209. def allow_sleep(dev, cookie):
  210.     '''Send a dbus signal to gnome-power-manager to allow a suspending
  211.   the system'''
  212.     
  213.     try:
  214.         dev.UnInhibit(cookie)
  215.     except Exception:
  216.         e = None
  217.         print 'could not send the dbus UnInhibit signal: %s' % e
  218.  
  219.  
  220.  
  221. def str_to_bool(str):
  222.     if str == '0' or str.upper() == 'FALSE':
  223.         return False
  224.     return True
  225.  
  226.  
  227. def utf8(str):
  228.     return unicode(str, 'latin1').encode('utf-8')
  229.  
  230.  
  231. def error(parent, summary, message):
  232.     import gtk
  233.     d = gtk.MessageDialog(parent = parent, flags = gtk.DIALOG_MODAL, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_CLOSE)
  234.     d.set_markup('<big><b>%s</b></big>\n\n%s' % (summary, message))
  235.     d.realize()
  236.     d.window.set_functions(gtk.gdk.FUNC_MOVE)
  237.     d.set_title('')
  238.     res = d.run()
  239.     d.destroy()
  240.     return False
  241.  
  242.  
  243. def humanize_size(bytes):
  244.     '''
  245.     Convert a given size in bytes to a nicer better readable unit
  246.     '''
  247.     if bytes == 0:
  248.         return _('0 KB')
  249.     if bytes < 1024:
  250.         return _('1 KB')
  251.     if bytes < 1048576:
  252.         return locale.format(_('%.0f KB'), bytes / 1024)
  253.     return locale.format(_('%.1f MB'), bytes / 1024 / 1024)
  254.  
  255. if __name__ == '__main__':
  256.     print mirror_from_sources_list()
  257.  
  258.